package com.scorpio.baen;

import com.alibaba.fastjson.JSONObject;
import com.scorpio.api.Constants;
import com.scorpio.server.base.MessagePullProcessor;

import java.nio.channels.SocketChannel;

public class UserInfo {
    public static final String USER_ID = "UserId";
    public static final String NETNAME = "Netname";
    public static final String TOKEN = "token";
    public static final String START = "start";
    public static final int START_US = -1;//未登录
    public static final int START_RE = 1;//已经登录
    public static final int START_CO = 2;//隐身
    private SocketChannel client;
    private MessagePullProcessor msgDistribute;
    private String userId;//用户id
    private String netname;//网名
    private String token;
    private long lastHearBeatTile = System.currentTimeMillis();
    //状态
    private int start = START_US;

    public UserInfo(SocketChannel client) {
        this.client = client;
        msgDistribute = new MessagePullProcessor(client);
    }

    public void addMessageBuffer(String msg) {
        msgDistribute.addMessage(msg);
    }

    public MessagePullProcessor getMsgDistribute() {
        return msgDistribute;
    }

    public String getUserId() {
        return userId == null ? "" : userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getNetname() {
        return netname == null ? "" : netname;
    }

    public void setNetname(String netname) {
        this.netname = netname;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public SocketChannel getClient() {
        return client;
    }

    public String getToken() {
        return token == null ? "" : token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public long getLastHearBeatTile() {
        return lastHearBeatTile;
    }

    public void setLastHearBeatTile(long lastHearBeatTile) {
        this.lastHearBeatTile = lastHearBeatTile;
    }

    /**
     * 获取心跳请求 数据体
     *
     * @return
     */
    public JSONObject getHeartBeatRequest() {
        JSONObject json = new JSONObject();
        json.put(Constants.RS_ACTION, Constants.ACTION_HEART_BEAT1);
        json.put(Constants.HB, Constants.HB_REQUEST);
        json.put(Constants.EL_TIME, System.currentTimeMillis() / 1000);
        return json;
    }

    /**
     * 获取心跳回执数据
     *
     * @param request_time 心跳请求时间
     * @return
     */
    public JSONObject getHeartBeatResult(long request_time) {
        JSONObject json = new JSONObject();
        json.put(Constants.RS_ACTION, Constants.ACTION_HEART_BEAT1);
        json.put(Constants.HB, Constants.HB_RESULT);
        json.put(Constants.EL_TIME, request_time);
        return json;
    }

    public void clearInfo() {
        msgDistribute.clear();
        userId = null;
        netname = null;
        start = START_US;
        lastHearBeatTile = System.currentTimeMillis();
    }
}
